#!/usr/bin/env python
#
# traffic_lights.py
#
# Version : 0.7
# Date : 20080520
#
# Author : Bernard Czenkusz
# Email  : bernie@skipole.co.uk

#
# traffic_lights.py - Displays a status window, and is a module in sm_gui
# package, run as part of the skipolemonitor gui interface
#
# Copyright (c) 2007 Bernard Czenkusz
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

""" This is a module in the sm_gui package, and defines the TrafficLights status frame

It is run as part of the skipolemonitor gui interface.
"""


import wx

from net import *
from sm_gui import gui_globals


# HOST_STATS is available via the net module
# HOST_STATS=[Total number of hosts, number of red hosts, yellow hosts, green hosts]
# used to display the icons showing the host status

# STATUS_BOX is 1 if the traffic lights status box is showing
STATUS_BOX=0


# ------------------ Class to display a Frame showing host counts

class TrafficLights(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "Status", style=wx.DEFAULT_FRAME_STYLE)
        #
        # Set the frame icon
        #
        self.SetIcon(gui_globals.get_icon())
        #
        global HOST_STATS
        global STATUS_BOX
        #
        # Flag that this Frame is displayed
        STATUS_BOX = 1
        #
        self.panel = wx.Panel(self, -1)
        #
        # Create a grid sizer, to hold three images, and alarm numbers
        self.traffic_sizer=wx.GridSizer(rows=0, cols=2, hgap=10, vgap=5)
        #
        font = wx.Font(24, wx.DEFAULT, wx.NORMAL, wx.BOLD)
        #
        # Initially - in each box, the total number of hosts HOST_STATS[0] is
        # used, rather than the red, yellow and green stats - this is so
        # the frame sizes to the largest possible number.
        #
        # RED HOSTS
        red_png=os.path.join(net_cfg["IMAGES_FOLDER"],'redhost.png')
        red_image= wx.Image(red_png, wx.BITMAP_TYPE_PNG)
        red_square=wx.StaticBitmap(self.panel, -1, wx.BitmapFromImage(red_image))
        self.traffic_sizer.Add(red_square, 0, wx.ALIGN_CENTER)
        self.red_text=wx.StaticText(self.panel, -1, " %i " % HOST_STATS[0])
        self.red_text.SetFont(font)
        self.traffic_sizer.Add(self.red_text, 0, wx.ALIGN_CENTER)
        #
        # YELLOW HOSTS
        yellow_png=os.path.join(net_cfg["IMAGES_FOLDER"],'yellowhost.png')
        yellow_image= wx.Image(yellow_png, wx.BITMAP_TYPE_PNG)
        yellow_square=wx.StaticBitmap(self.panel, -1, wx.BitmapFromImage(yellow_image))
        self.traffic_sizer.Add(yellow_square, 0, wx.ALIGN_CENTER)
        self.yellow_text=wx.StaticText(self.panel, -1, " %i " % HOST_STATS[0])
        self.yellow_text.SetFont(font)
        self.traffic_sizer.Add(self.yellow_text, 0, wx.ALIGN_CENTER)
        #
        # GREEN HOSTS
        green_png=os.path.join(net_cfg["IMAGES_FOLDER"],'greenhost.png')
        green_image= wx.Image(green_png, wx.BITMAP_TYPE_PNG)
        green_square=wx.StaticBitmap(self.panel, -1, wx.BitmapFromImage(green_image))
        self.traffic_sizer.Add(green_square, 0, wx.ALIGN_CENTER)
        self.green_text=wx.StaticText(self.panel, -1, " %i " % HOST_STATS[0])
        self.green_text.SetFont(font)
        self.traffic_sizer.Add(self.green_text, 0, wx.ALIGN_CENTER)
        #
        # Set the size to the panel, and frame
        self.panel.SetSizer(self.traffic_sizer)
        self.traffic_sizer.Fit(self)
        self.traffic_sizer.SetSizeHints(self)
        #
        # Now, after the sizing is done, the correct stats are inserted
        #
        self.red_text.SetLabel("%i" % HOST_STATS[1])
        self.yellow_text.SetLabel("%i" % HOST_STATS[2])
        self.green_text.SetLabel("%i" % HOST_STATS[3])
        #
        # Check status every twenty seconds
        self.status_checker = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnChecking, self.status_checker)
        self.status_checker.Start(20000, oneShot=False)
        #
        # Bind the close event
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

    def OnChecking(self, event):
        """Check the host statisticts"""
        global HOST_STATS
        # set the values on the mini traffic lights window
        self.red_text.SetLabel("%i" % HOST_STATS[1])
        self.yellow_text.SetLabel("%i" % HOST_STATS[2])
        self.green_text.SetLabel("%i" % HOST_STATS[3])

    def OnCloseWindow(self, event):
        """Frame closer"""
        global STATUS_BOX
        # This global variable is used to flag this frame is now off
        STATUS_BOX = 0
        self.Destroy()


